package com.xu.chuang.show.ui;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.MultiAutoCompleteTextView;

import com.xu.chuang.show.R;
import com.xu.chuang.show.bean.Content;
import com.xu.chuang.show.bean.ShowUser;
import com.xu.chuang.show.network.SyncRequest;
import com.xu.chuang.show.service.impl.MainBizReqImpl;
import com.xu.chuang.show.service.inte.MainBizReqService;

public class SendContextActivity extends AppCompatActivity {

    public static final String ARG_PARAM = "param";

    public static final String PARAM_LAT = "param_lat";

    public static final String PARAM_LOG = "param_lon";

    private Double localLatitude;

    private Double localLongitude;

    ShowUser showUser;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_context);

        SyncRequest.syncReq();
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        showUser = (ShowUser) bundle.get(ARG_PARAM);
        localLatitude = bundle.getDouble(PARAM_LAT);
        localLongitude = bundle.getDouble(PARAM_LOG);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle(R.string.love_saying);
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.drawable.ic_cancel);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        Button commitButton = (Button) findViewById(R.id.context_commit);
        commitButton.setOnClickListener(contextCommitClick);
    }

    private Button.OnClickListener contextCommitClick = new Button.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(commitAble()) {
                Intent intent = new Intent(v.getContext(), WingActivity.class);
                startActivity(intent);
            }
        }
    };

    private boolean commitAble() {
        MultiAutoCompleteTextView contextEditView = (MultiAutoCompleteTextView)
                        findViewById(R.id.et_send_context);
        String contextString = contextEditView.getText().toString();

        boolean cancel = false;
        View focusView = null;

        if (TextUtils.isEmpty(contextString)) {
            contextEditView.setError(getString(R.string.error_field_required));
            focusView = contextEditView;
            cancel = true;
        }

        if (cancel) {
            focusView.requestFocus();
        } else {
            Content content = new Content();
            content.setContext(contextString);
            content.setLatitude(localLatitude);
            content.setLongitude(localLongitude);
            content.setShowUserId(showUser.getShowUserId());
            MainBizReqService mainBizReqService = new MainBizReqImpl();
            mainBizReqService.upContent(content);
        }

        return !cancel;
    }
}
